//
//  CarDataCell.m
//  Cycling
//
//  Created by cjg.mini on 2023/4/3.
//

#import "CarDataCell.h"

@implementation CarDataCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
    if (highlighted) {
        self.contentView.backgroundColor = COLOR_WHITE;
    }else{
        self.contentView.backgroundColor = COLOR_WHITE;
    }
    [super setHighlighted:highlighted animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = COLOR_WHITE;
        [self setupUI];
    }
    return self;
}

- (void)setupUI {

    self.carNameLB = [[UILabel alloc] init];
    self.carNameLB.font = FONT_SECONDARY_TITLE;
    self.carNameLB.textColor = COLOR_BLACK;
    self.carNameLB.text = @"bike01";
    [self.contentView addSubview:self.carNameLB];
    [self.carNameLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        make.top.equalTo(self.contentView).offset(MARGIN_MIDDLE);
        make.height.mas_equalTo(20);
    }];
    
    self.totalMileTitleLB = [[UILabel alloc] init];
    self.totalMileTitleLB.font = FONT_BODY_15;
    self.totalMileTitleLB.textColor = COLOR_NOT_SELECT;
    self.totalMileTitleLB.numberOfLines = 0;
    self.totalMileTitleLB.text = CycLocalizedString(@"Mileage", @"骑行总里程");
    [self.contentView addSubview:self.totalMileTitleLB];
    [self.totalMileTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        make.top.equalTo(self.carNameLB.mas_bottom).offset(MARGIN_MIDDLE);
        make.width.mas_equalTo(80);
    }];
    
    self.totalMileNumLB = [[UILabel alloc] init];
    self.totalMileNumLB.font = FONT_MEDIUM_28;
    self.totalMileNumLB.textColor = COLOR_BLACK;
    self.totalMileNumLB.text = @"0.00";
    [self.contentView addSubview:self.totalMileNumLB];
    [self.totalMileNumLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(MARGIN_MAX);
        make.top.equalTo(self.carNameLB.mas_bottom).offset(MARGIN_MIDDLE * 2 + 50);
        make.top.greaterThanOrEqualTo(self.totalMileTitleLB.mas_bottom).offset(MARGIN_MIDDLE);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];


    self.leftMileTitleLB = [[UILabel alloc] init];
    self.leftMileTitleLB.font = FONT_BODY_15;
    self.leftMileTitleLB.textColor = COLOR_NOT_SELECT;
    self.leftMileTitleLB.numberOfLines = 0;
    self.leftMileTitleLB.text = CycLocalizedString(@"Remaining miles", @"剩余里程");
    [self.contentView addSubview:self.leftMileTitleLB];
    [self.leftMileTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalMileTitleLB.mas_right).offset(MARGIN_MIDDLE);
        make.top.equalTo(self.totalMileTitleLB);
        make.width.mas_equalTo(80);
    }];

    self.leftMileNumLB = [[UILabel alloc] init];
    self.leftMileNumLB.font = FONT_MEDIUM_28;
    self.leftMileNumLB.textColor = COLOR_BLACK;
    self.leftMileNumLB.text = @"0.00";
    [self.contentView addSubview:self.leftMileNumLB];
    [self.leftMileNumLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.totalMileTitleLB.mas_right).offset(MARGIN_MIDDLE);
        make.top.greaterThanOrEqualTo(self.leftMileTitleLB.mas_bottom).offset(MARGIN_MIDDLE);
        make.top.and.height.equalTo(self.totalMileNumLB);
        make.width.mas_equalTo(80);
    }];

    self.singleMileTitleLB = [[UILabel alloc] init];
    self.singleMileTitleLB.font = FONT_BODY_15;
    self.singleMileTitleLB.textColor = COLOR_NOT_SELECT;
    self.singleMileTitleLB.numberOfLines = 0;
    self.singleMileTitleLB.text = CycLocalizedString(@"Longest ride", @"最长单次骑行");
    [self.contentView addSubview:self.singleMileTitleLB];
    [self.singleMileTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftMileTitleLB.mas_right).offset(MARGIN_MIDDLE);
        make.top.equalTo(self.totalMileTitleLB);
        make.width.mas_equalTo(80);
    }];

    self.singleMileNumLB = [[UILabel alloc] init];
    self.singleMileNumLB.font = FONT_MEDIUM_28;
    self.singleMileNumLB.textColor = COLOR_BLACK;
    self.singleMileNumLB.text = @"0.00";
    [self.contentView addSubview:self.singleMileNumLB];
    [self.singleMileNumLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftMileTitleLB.mas_right).offset(MARGIN_MIDDLE);
        make.top.greaterThanOrEqualTo(self.singleMileTitleLB.mas_bottom).offset(MARGIN_MIDDLE);
        make.top.and.height.equalTo(self.totalMileNumLB);
        make.width.mas_equalTo(80);
    }];

    self.batteryTitleLB = [UILabel new];
    self.batteryTitleLB.font = FONT_BODY_15;
    self.batteryTitleLB.textColor = COLOR_NOT_SELECT;
    self.batteryTitleLB.numberOfLines = 0;
    self.batteryTitleLB.text = CycLocalizedString(@"Battery", @"剩余电量");
    [self.contentView addSubview:self.batteryTitleLB];
    [self.batteryTitleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.singleMileTitleLB.mas_right).offset(MARGIN_MIDDLE);
        make.top.equalTo(self.totalMileTitleLB);
        make.right.mas_equalTo(-MARGIN_MIDDLE);
//        make.height.mas_equalTo(50);
    }];
    
    self.batteryNumLB = [[UILabel alloc] init];
    self.batteryNumLB.font = FONT_MEDIUM_28;
    self.batteryNumLB.textColor = COLOR_BLACK;
    self.batteryNumLB.text = @"0.00";
    [self.contentView addSubview:self.batteryNumLB];
    [self.batteryNumLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.batteryTitleLB);
        make.top.greaterThanOrEqualTo(self.batteryTitleLB.mas_bottom).offset(MARGIN_MIDDLE);
        make.top.and.height.equalTo(self.totalMileNumLB);
    }];
//    self.batteryView = [[BatteryView alloc] initWithFrame:CGRectZero];
//    [self.contentView addSubview:self.batteryView];
//    [self.batteryView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(self.contentView).offset(-MARGIN_MAX);
//        make.top.equalTo(self.contentView).offset(MARGIN_MIDDLE);
//        make.height.mas_equalTo(120);
//        make.width.mas_equalTo(75);
//    }];
}

- (void)setModel:(Equipment *)model {
    _model = model;
    
    self.totalMileNumLB.text = model.totalmileage.length ? model.totalmileage : @"0.0";
    self.leftMileNumLB.text = model.canmileage.length ? model.canmileage : @"0.0";
    self.singleMileNumLB.text = model.danci.length ? model.danci : @"0.0";
    self.batteryNumLB.text =  [NSString stringWithFormat:@"%.0f%%", [model.electricity floatValue]];
//    [self.batteryView setBattery:[model.electricity floatValue]/100.0];
}

@end
